﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper;
using VehicleManagement.ControllersServices;
using System.Collections;

namespace VehicleManagement.Controllers
{
    //[CustomerAuthorize(Roles = "负责人")]
    public class AddGasController : Controller
    {
        const int PAGE_MAX = 8;
        //
        // GET: /AddGas/
        AddGasService addGasService = new AddGasService();

        public ActionResult Index()
        {
            Hashtable ht = new Hashtable();
            SearchAddGasByRequeast(ref ht);
            int page;
            if (!string.IsNullOrWhiteSpace(Request.Params["Page"]))
                page = int.Parse(Request.Params["Page"]);
            else
                page =1;

            if (Session["CompanyName"] == null)
            {
                return View("../Home/Login");
            }
            ht.Add("companyName", Session["CompanyName"].ToString());
            return View(addGasService.GetAllAddGas(ht,(page - 1) * PAGE_MAX, PAGE_MAX));
        }

        //
        // GET: /AddGas/Details/5
        /// <summary>
        /// 根据加油ID，查看加油详细信息，并返回视图
        /// </summary>
        /// <param name="id">加油的ID</param>
        /// <returns>加油信息详细视图</returns>
        public ActionResult Details(int id)
        {
            Hashtable ht = new Hashtable();
            if (Session["CompanyName"] == null)
            {
                return View("../Home/Login");
            }
            ht.Add("companyName", Session["CompanyName"].ToString());
            ht.Add("id", id);
            return View(addGasService.AddGasDetail(ht));
        }

        //
        // GET: /AddGas/Create

        public ActionResult Create()
        {
            return View();        
        } 

        //
        // POST: /AddGas/Create

        [HttpPost]
        public ActionResult Create(AddGas addGas)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    addGas.CompanyName = Session["CompanyName"].ToString();
                    object obj = addGasService.InsertAddGasEx(addGas);
                    return RedirectToAction("Index");
                }
                return View(addGas);                
            }
            catch (Exception)
            {
                return View();
            }
        }

        /// <summary>
        /// 车加油信息修改
        /// </summary>
        /// <param name="id">车加油ID</param>
        /// <param name="driveriofo">返回车加油类</param>
        /// <returns>修改车加油的视图</returns>
 
        public ActionResult Edit(int id)
        {
            Hashtable ht = new Hashtable();
            if (Session["CompanyName"] == null)
            {
                return View("../Home/Login");
            }
            ht.Add("companyName", Session["CompanyName"].ToString());
            ht.Add("id", id);
            return View(addGasService.ShowEdit(ht));
        }

        //
        // POST: /AddGas/Edit/5

        [HttpPost]
        public ActionResult Edit(int id,  AddGas addgas)
        {
            try
            {
                addGasService.EditAddGas(addgas); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AddGas/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /AddGas/Delete/5

        [HttpPost, ActionName("DeleteAddGas")]
        public ActionResult DeleteConfirmed(int id, FormCollection collection)
        {
            try
            {
                int i = addGasService.DeleteAddGas(id);
                if (i == 1)//删除成功
                {
                    return Json(new { Result = "true", Message = "删除成功" });
                }
                else//删除不成功
                {
                    return Json(new { Result = "false", Message = "删除失败" });
                }
            }
            catch
            {
                return View();
            }
        }

        [HttpGet,ActionName("GetPagingInfo")]
        public ActionResult GetPagingInfo()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request.Params["Page"]))
                currentPage = 1;
            else
                currentPage = int.Parse(Request.Params["Page"]);

            Hashtable ht = new Hashtable();
            SearchAddGasByRequeast(ref ht);
            int itemCount = addGasService.CountAllAddGas(ht);
            return Json(new
            {
                TotalItems = itemCount,
                ItemsPerPage = PAGE_MAX,
                CurrentPage = currentPage,
                TotalPages = (int)Math.Ceiling((decimal)itemCount / PAGE_MAX)
            },JsonRequestBehavior.AllowGet);
        }

        [NonAction]
        public void SearchAddGasByRequeast(ref Hashtable ht)
        {
            string addgasCar_num = Request.Params["Car_num"];
            string addgasGas_Name = Request.Params["Gas_Name"];
            string applyState = Request.Params["Approval"];
            string AddGasCar_numForSearch = "%%";
            string AddGasGas_NameForSearch = "%%";
            int ApprovalStateForSearch = 3;

            if (applyState == "已审批")
                ApprovalStateForSearch = 1;
            else if (applyState == "未审批")
                ApprovalStateForSearch = 0;

            if (!string.IsNullOrEmpty(addgasCar_num) && addgasCar_num != "车牌号")
                AddGasCar_numForSearch = string.Format("%{0}%", addgasCar_num);
            if (!string.IsNullOrEmpty(addgasGas_Name) && addgasGas_Name != "燃油名称")
                AddGasGas_NameForSearch = string.Format("%{0}%", addgasGas_Name);

            ht.Add("AddGasCar_numForSearch", AddGasCar_numForSearch);
            ht.Add("AddGasGas_NameForSearch", AddGasGas_NameForSearch);
            ht.Add("ApprovalStateForSearch", ApprovalStateForSearch);
        }


    }
}
